<?php
App::uses('AppModel', 'Model');
/**
 * Tarefa Model
 *
 * @property Projetos $Projetos
 */
class Tarefa extends AppModel {
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_tarefas';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'nome';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_tarefas' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'projetos_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'status' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'nome' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'descricao' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'prioridade' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Projetos' => array(
			'className' => 'Projeto',
			'foreignKey' => 'projetos_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Status' => array(
			'className' => 'Status',
			'foreignKey' => 'status_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'usuario_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);
	
	public $hasMany = array(
		'Mensagem' => array(
			'className' => 'Mensagem',
			'foreignKey' => 'id_mensagens',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'NaoLido' => array(
			'className' => 'NaoLido',
			'foreignKey' => 'id',
			'dependent' => false,
		)
	);
	
	function afterFind($results){
		foreach($results as &$item){
			if(isset($item['Tarefa']['updated']))
				$item['Tarefa']['updated_view'] = $this->dataView($item['Tarefa']['updated']);
		}
		return $results;
	}
	
	function dataView($data){
		$temp = explode('-', $data);
		$hora = explode(' ', $temp[2]);
		
		return $hora[1].' '.$hora[0].'/'.$temp[1].'/'.$temp[0];
	}
	
	function listaMensagems($result){
		$return[] = $this->Mensagem->find('first', array('conditions'=>array('tarefas_id'=>$result['Tarefa']['id_tarefas'])));
		for($i=0;$return[$i]['Mensagem']['proxima_msg'] != 0;$i++){
			$return[] = $this->Mensagem->find('first', array('conditions'=>array('id_mensagens'=>$return[$i]['Mensagem']['proxima_msg'])));
		}
		return ($return);
	}
	
	function ultimaMensagem($resultados){
		foreach ($resultados as &$item){
			$item['UltimaMensagem'] = $this->Mensagem->ultimaMensagem($item['Tarefa']['id_tarefas']);
			if(empty($item['UltimaMensagem']['User'])){
				$item['UltimaMensagem']['User'] = $item['User'];
			}
		}
		return $resultados;
	}
	
	function nomeDoProjeto($id){
		return($this->Projetos->findByIdProjetos($id, array('fields'=>'Projetos.nome')));
	}
	
	function novasMensagens($id, $idMensagem, $usuario){
		$tarefa = $this->findByIdTarefas($id, array('fields'=>'Projetos.id_projetos'));
		return $this->Projetos->novasMensagens($tarefa['Projetos']['id_projetos'], $idMensagem, $id, $usuario);
	}
	
	function apagaMensagensNaoLidas($idTarefa, $usuario){
		$naoLidas = $this->NaoLido->find('list', array('conditions'=>array('tarefas_id'=>$idTarefa, 'users_id'=>$usuario['id_users'])));
		foreach($naoLidas as $item){
			$this->NaoLido->delete($item);
		}
	}
}
